package com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.system.transfer.UserDto;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.dao.ConsultantResListDao;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.ConsultantResListInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.SearchConsultantResListInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.IConsultantResListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ConsultantResListServiceImpl implements IConsultantResListService {
    @Autowired
    private ConsultantResListDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<ConsultantResListInfo> selectPageListByQuery(Page<ConsultantResListInfo> page, SearchConsultantResListInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsDone())) {
            params.put("isDone", searchInfo.getIsDone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        String sql = "select roleid from user where id='" + ShiroKit.getUser().getId() + "'";
        UserDto userDto = sqlMapper.selectOne(sql, UserDto.class);
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if ("14".equals(userDto.getRoleid())) {
            params.put("fieldTc", "");
            params.put("appointmentPayService", "洁牙");
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDiagnosisStatus())) {
            params.put("diagnosisStatus", searchInfo.getDiagnosisStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已结账".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "t.deal_status ='已结账'");
            } else {
                params.put("dealStatus", "t.deal_status !='已结账'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        params.put("tableName", ShiroKit.getUser().getCustomer_info());
        params.put("tableName1", tableName1);
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPagets(page, params);
    }

    public int getQueryPagesCount(Page<ConsultantResListInfo> page, SearchConsultantResListInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsDone())) {
            params.put("isDone", searchInfo.getIsDone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        String sql = "select roleid from user where id='" + ShiroKit.getUser().getId() + "'";
        UserDto userDto = sqlMapper.selectOne(sql, UserDto.class);
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if ("14".equals(userDto.getRoleid())) {
            params.put("fieldTc", "");
            params.put("appointmentPayService", "洁牙");
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDiagnosisStatus())) {
            params.put("diagnosisStatus", searchInfo.getDiagnosisStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已结账".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "t.deal_status ='已结账'");
            } else {
                params.put("dealStatus", "t.deal_status !='已结账'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        params.put("tableName", ShiroKit.getUser().getCustomer_info());
        params.put("tableName1", tableName1);
        return dao.getQueryPagesCount(page, params);
    }

    @Override
    public List<ConsultantResListInfo> consultantExpor(SearchConsultantResListInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getStartAppointmentTime())) {
            params.put("startAppointmentTime", searchInfo.getStartAppointmentTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndAppointmentTime())) {
            params.put("endAppointmentTime", searchInfo.getEndAppointmentTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        String sql = "select roleid from user where id='" + ShiroKit.getUser().getId() + "'";
        UserDto userDto = sqlMapper.selectOne(sql, UserDto.class);
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if ("14".equals(userDto.getRoleid())) {
            params.put("fieldTc", "");
            params.put("appointmentPayService", "洁牙");
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDiagnosisStatus())) {
            params.put("diagnosisStatus", searchInfo.getDiagnosisStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已结账".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "t.deal_status ='已结账'");
            } else {
                params.put("dealStatus", "t.deal_status !='已结账'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        params.put("tableName", ShiroKit.getUser().getCustomer_info());
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        params.put("tableName1", tableName1);
        return dao.getQueryPages2(params);
    }
}